package com.example.sharding.jdbc.demo.util;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
 * @author airtrioa
 * @date 2021/11/30
 */
public class DateUtil {
  private DateUtil() {
  }

  public static Date toDate(LocalDateTime localDateTime) {
    // 获取时间地区ID
    ZoneId zoneId = ZoneId.systemDefault();
    // 转换为当地时间
    ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
    // 转换为Date类型
    return Date.from(zonedDateTime.toInstant());
  }

  public static LocalDateTime toLocalDateTime(Date date) {
    return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
  }
}
